package com.qj.platform.pay.controller;

import com.qj.base.commons.domain.SearchObject;
import com.qj.base.commons.utils.JsonUtils;
import com.qj.base.controller.BaseController;
import com.qj.base.domain.ResultTemplate;
import com.qj.platform.pay.core.domain.Order;
import com.qj.platform.pay.core.service.OrderService;
import com.qj.platform.pay.mssm.domain.MSNotifyResponse;
import com.qj.platform.pay.mssm.domain.MSSMResponse;
import com.qj.platform.pay.mssm.utils.MSSMPayPlatformUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhijie on 2016/12/15.
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController{
@Autowired
    private OrderService orderService;

    @RequestMapping("/_add")
    public ResultTemplate insert(Order order){
        return getSuccessTemplate(orderService.insert(order));
    }

    @RequestMapping("/_paySM")
    public ResultTemplate paySM(String merchantId,long totalAmount){
        return getSuccessTemplate(orderService.paySM(merchantId, totalAmount));
    }

    @RequestMapping("/_update")
    public ResultTemplate update(Order order){
        orderService.update(order);
        return getSuccessTemplate();
    }

    @RequestMapping("/_get")
    public ResultTemplate get(String id){
        Order order= orderService.getById(id);
        return getSuccessTemplate(order);
    }

    @RequestMapping("/_query")
    public ResultTemplate query(SearchObject searchObject){
        return getSuccessTemplate(orderService.query(searchObject),searchObject.getPageNum(),searchObject.getPageSize(),
                orderService.queryCount(searchObject));
    }


    @RequestMapping("/_tradeNotify")
    public String tradeNotify(MSSMResponse response,String signData, String tranCode, String reqMsgId, String ext, HttpServletRequest request){
        System.out.println(JsonUtils.toJson(request.getParameterMap()));
//        response.setEncryptData(response.getEncryptData().replace(" ","+"));
//        response.setEncryptKey(response.getEncryptKey().replace(" ","+"));
//        response.setEncryptData("I2K9zWFBB/8IoNhC1uLBzxEiH6sQedVw/kbDcsePTMnHECVxe7WkpA5VXKbYtvezdG3jhkWspgeQ1cPcYiEfkei9EMeq+mo0mJ5AqfTD00gM8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8E6Aabk6ZTv4GwE3qDyXOMPJtDM9up0F+ZujlX2eZIe6LsPZJ0VVZmrDip2JT8PZGrfo+F06963vWvx1R6FEMdiDUeIyC+5Rpzi6TikaosxP8nkCkQUNdn0gBxITej+/QUrviE/VYSCXaT9VsBFnK4qojNuEzINJQe/rZlhKreZ/6LBimtAb2kOsInodzJrPm7LiAm6ygFA4xNc3lgEfGmQrGIlQ==");
        MSNotifyResponse msNotifyResponse= MSSMPayPlatformUtil.decryptToObj(response,MSNotifyResponse.class);
        orderService.insert(msNotifyResponse.getBody());
        orderService.handelNotify(msNotifyResponse.getBody());
        return "000000";
    }
}
